package pre.lxm.demo.rollview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;


public class SizeChangeView extends View {

    private int mLineCount = 3;
    private Square[] mSquares;
    private Paint mPaint;
    private int mWidth, mHeight;
    private int dividerMargin = 40;

    int squareWidth;
    private TextPaint mTextPaint;

    public SizeChangeView(Context context) {
        this(context, null);
    }

    public SizeChangeView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SizeChangeView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mPaint = new Paint();
        mPaint.setStrokeWidth(5);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setColor(Color.YELLOW);

        mTextPaint = new TextPaint();
        mTextPaint.setColor(Color.BLUE);
        mTextPaint.setTextSize(60);
        mTextPaint.setStrokeWidth(4);
        mTextPaint.setTextAlign(Paint.Align.CENTER);
        mTextPaint.setStyle(Paint.Style.FILL);
        initSquare();
    }

    private void initSquare() {
        mSquares = new Square[mLineCount * mLineCount];
        for (int i = 0; i < mSquares.length; i++) {
            mSquares[i] = new Square();
            mSquares[i].index = String.valueOf(i);
            mSquares[i].rectF = new RectF();
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawColor(Color.parseColor("#E1E1E1"));

        for (int i = 0; i < mSquares.length; i++) {
            RectF rectF = mSquares[i].rectF;
            canvas.drawRect(rectF, mPaint);

            Rect bounds = new Rect();
            mTextPaint.getTextBounds(mSquares[i].index, 0, 1, bounds);
            Paint.FontMetricsInt fontMetrics = mTextPaint.getFontMetricsInt();
            int xNum = (i + 1) % 3 == 0 ? 3 : (i + 1) % 3;
            int yNum = i / 3 + 1;
            Log.e(i + "--", "xNum:" + xNum + ",\tyNum:" + yNum);
            canvas.save();
            canvas.translate(dividerMargin * xNum + (xNum - 1) * squareWidth, dividerMargin * yNum + (yNum - 1) *
                    squareWidth);
            float baseline = rectF.height() / 2 + (fontMetrics.descent - fontMetrics.ascent) / 2 - fontMetrics.descent;
            canvas.drawText(mSquares[i].index, rectF.width() / 2 - bounds.width() / 2 + dividerMargin / 2, baseline,
                    mTextPaint);
            canvas.restore();
        }
    }


    public void change(int firstPos) {

    }


    private void fixSquarePosition(Square[] squares) {
        Log.e("---", "mWidth:" + mWidth + ",\tmHeight:" + mHeight + ",\tdividerMargin:" + dividerMargin);
        squareWidth = (mWidth - (mLineCount + 1) * dividerMargin) / mLineCount;
        Log.e("---", "squareWidth:" + squareWidth);
        int left_0 = dividerMargin;
        int top_0 = dividerMargin;
        squares[0].rectF.set(left_0, top_0, left_0 + squareWidth, top_0 + squareWidth);
        for (int i = 1; i < squares.length; i++) {
            int left = left_0 + i % mLineCount * (dividerMargin + squareWidth);
            int top = top_0 + i / mLineCount * (dividerMargin + squareWidth);
            int right = left + squareWidth;
            int bottom = top + squareWidth;
            squares[i].rectF.set(left, top, right, bottom);
        }

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mWidth = MeasureSpec.getSize(widthMeasureSpec);
        mHeight = MeasureSpec.getSize(heightMeasureSpec);
        if (mWidth > mHeight) {
            mWidth = mHeight;
        }
        if (mHeight > mWidth) {
            mHeight = mWidth;
        }
        fixSquarePosition(mSquares);
        setMeasuredDimension(mWidth, mHeight);
    }


    private class Square {
        RectF rectF;
        String index;
    }
}
